package config

import (
	"errors"
	"fmt"
	"github.com/spf13/viper"
	"os"
)

type ConfigStruct struct {
	Server   ServerConfig `yaml:"apiserver"`
	Dbmaster DbConfig     `yaml:"dbmaster"`
	Dbsecond DbConfig     `yaml:"dbsecond"`
	Logger   LogConfig    `yaml:"logger"`
	Redis    RedisConfig  `yaml:"Redis"`
	Mongo    MongoConfig  `yaml:"Mongo"`
	File     FileConfig   `yaml:"file"`
	Cache    CacheConfig  `yaml:"cache"`
}

var (
	configFile = "./config.yaml"
	Config     = ConfigStruct{}
)

func Init(configfullname string) {
	if configfullname != "" {
		configFile = configfullname
	}
	_, err := os.Stat(configFile)
	if err != nil {
		fmt.Println(errors.New("配置文件不存在"))
		os.Exit(0)
	}
	viper.SetConfigFile(configFile)
	if err := viper.ReadInConfig(); err != nil {
		fmt.Println(errors.New("配置文件格式错误"), err)
		os.Exit(0)
		return
	}

	err = viper.Unmarshal(&Config)
	if err != nil {
		panic(err)
	}
	fmt.Println("*************加载配置文件完成************")
	//viper.WatchConfig()
	fmt.Println("Mongo", Config.Mongo.URI)
}
